package chat.whiteboard;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

import chat.whiteboard.components.ArrowButton;
import chat.whiteboard.components.ChatInputTextField;
import chat.whiteboard.components.ChatTextArea;
import chat.whiteboard.components.CircleButton;
import chat.whiteboard.components.ColorComboBox;
import chat.whiteboard.components.FontComboBox;
import chat.whiteboard.components.ICommand;
import chat.whiteboard.components.LineButton;
import chat.whiteboard.components.RectangleButton;
import chat.whiteboard.components.SendButton;
import chat.whiteboard.components.canvas.CanvasMouseAction;
import chat.whiteboard.components.canvas.JCanvas;
import chat.whiteboard.components.legend.LegendList;
import chat.whiteboard.models.UserModel;
import chat.whiteboard.utils.classes.CustomColor;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dobre
 */
public class WhiteBoardView extends JPanel implements ActionListener{
	private WhiteBoardController controller;

	private JButton btnArrow;
    private JButton btnCircle;
    private JButton btnLine;
    private JButton btnRect;
    private JButton btnSend;
    private JCanvas canvas;
    private JComboBox cbColor;
    private JComboBox cbFont;
    private JButton btnColor;
    private JTextField tfChatInput;
    private LegendList legend_list;
    private DefaultListModel<UserModel> legend_model;
    
    // Variables declaration - do not modify
    private JButton btnFont;
    private JTextPane chatTextArea;
    private JScrollPane jScrollPane1;
    private JScrollPane legendScrollPane;
    private JToolBar toolBar;
    // End of variables declaration
	
    /**
     * Creates new form WhiteBoard
     */
    public WhiteBoardView(UserModel owner) {
        initComponents();
        controller.setCurrentUser(owner);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    	controller = new WhiteBoardController();
    	btnRect		= new RectangleButton(this,controller);
    	btnCircle 	= new CircleButton(this,controller);
    	btnArrow 	= new ArrowButton(this,controller);
    	btnLine 	= new LineButton(this,controller);
    	tfChatInput = new ChatInputTextField(this,controller);
    	btnSend 	= new SendButton(this,controller);
    	cbFont 		= new FontComboBox(this,controller);
    	cbColor		= new ColorComboBox(this,controller);
    	
    	// Canvas
    	canvas 		= new JCanvas(controller);
    	canvas.addMouseListener(new CanvasMouseAction(canvas));
    	
    	// Legend List
    	legend_list = new LegendList(controller);
    	legendScrollPane = new JScrollPane(legend_list);

    	toolBar = new JToolBar();
        btnFont = new JButton();
        btnColor = new JButton();
        jScrollPane1 = new JScrollPane();
        chatTextArea = new ChatTextArea(this,controller);

        toolBar.setRollover(true);
        toolBar.setName("toolbar");

        btnRect.setText("Rect");
        btnRect.setFocusable(false);
        btnRect.setHorizontalTextPosition(SwingConstants.CENTER);
        btnRect.setName("btn_rectangle");
        btnRect.setVerticalTextPosition(SwingConstants.BOTTOM);
        toolBar.add(btnRect);

        btnCircle.setText("Circle");
        btnCircle.setFocusable(false);
        btnCircle.setHorizontalTextPosition(SwingConstants.CENTER);
        btnCircle.setName("btn_circle");
        btnCircle.setVerticalTextPosition(SwingConstants.BOTTOM);
        toolBar.add(btnCircle);

        btnArrow.setText("Arrow");
        btnArrow.setFocusable(false);
        btnArrow.setHorizontalTextPosition(SwingConstants.CENTER);
        btnArrow.setName("btn_arrow");
        btnArrow.setVerticalTextPosition(SwingConstants.BOTTOM);
        toolBar.add(btnArrow);

        btnLine.setText("Line");
        btnLine.setFocusable(false);
        btnLine.setHorizontalTextPosition(SwingConstants.CENTER);
        btnLine.setName("btn_line");
        btnLine.setVerticalTextPosition(SwingConstants.BOTTOM);
        toolBar.add(btnLine);

        btnFont.setText("Font");

        btnColor.setText("Color");

        cbFont.setModel(new DefaultComboBoxModel(new Integer[] {8,10,12,14,16,20}));
        cbFont.setSelectedIndex(2);
        cbColor.setModel(new DefaultComboBoxModel(new CustomColor[] { 
        		new CustomColor("Black"	, Color.BLACK), 
        		new CustomColor("Red"	, Color.RED),
        		new CustomColor("Green"	, Color.GREEN),
        		new CustomColor("Yellow", Color.YELLOW),
        		new CustomColor("Orange", Color.ORANGE),
        		new CustomColor("Pink"	, Color.PINK),
        		new CustomColor("Cyan"	, Color.CYAN)
        		}));

        btnSend.setText("Send");

        canvas.setBackground(new java.awt.Color(255, 255, 255));

        GroupLayout canvasLayout = new GroupLayout(canvas);
        canvas.setLayout(canvasLayout);
        canvasLayout.setHorizontalGroup(
            canvasLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 297, Short.MAX_VALUE)
        );
        canvasLayout.setVerticalGroup(
            canvasLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(chatTextArea);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
                            .addComponent(canvas, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31)
                        .addComponent(legendScrollPane, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnFont)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbFont, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnColor)
                        .addGap(18, 18, 18)
                        .addComponent(cbColor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfChatInput, GroupLayout.PREFERRED_SIZE, 365, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSend))
                    .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 417, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(canvas, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(legendScrollPane, GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFont)
                    .addComponent(btnColor)
                    .addComponent(cbFont, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbColor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(tfChatInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSend))
                .addContainerGap())
        );
    }// </editor-fold>

    public IWhiteBoardController getController(){
    	return controller;
    }
	@Override
	public void actionPerformed(ActionEvent e) {
		ICommand com = (ICommand)e.getSource();
		com.execute();
	}
}
